﻿using System.Windows.Controls;
using System.Windows.Input;
using Renren.Helpers;
using Renren.Models;
using Renren.ViewModels;
using System.Windows.Media.Imaging;
using System;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Data;
using System.Collections.Generic;

namespace Renren.Controls
{
    public partial class NewsBox : UserControl
    {
        public static readonly DependencyProperty TitleSpanProperty =
            DependencyProperty.Register(
            "TitleSpan",
            typeof(string),
            typeof(NewsBox),
            new PropertyMetadata(string.Empty, TitleChanged));

        static public bool ReactEvent { get; set; }

        private NewsViewModel NewsView
        {
            get { return (NewsViewModel)DataContext; }
        }

        public NewsBox()
        {
            if (!ReactEvent)
            {
                ReactEvent = true;
            }
            InitializeComponent();
            ReactEvent = true;
            this.HeadPhoto.Tap += (s, e) => this.Goto("UserPage", NewsView.Model.ActorId);
            this.NameField.Tap += (s, e) => this.Goto("UserPage", NewsView.Model.ActorId);
            this.CommentField.Tap += (s, e) => this.Goto("NewsPage", NewsView.Model);
            this.PhotosField.Tap += (s, e) => this.Goto("PhotoPage", NewsView.Model);
            this.RichTitleField.Tap += this.NavigateToContentPage;
            this.ContentField.Tap += this.NavigateToContentPage;
            var b = new Binding("Title");
            SetBinding(TitleSpanProperty, b);
        }

        static private void TitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && (e.NewValue as string) != string.Empty && e.NewValue != e.OldValue)
            {
                (d as NewsBox).RichTitle.Inlines.Clear();

                var tokens = (e.NewValue as string).Split("()".ToCharArray());

                for (int i = 0; i < tokens.Length; i++)
                {
                    if (i % 2 == 0 && tokens[i] != string.Empty)
                    {
                        var run = new Run();
                        run.Text = tokens[i];
                        (d as NewsBox).RichTitle.Inlines.Add(run);
                    }
                    else if (i % 2 == 1 && Emotions.GetUri(tokens[i]) != string.Empty)
                    {
                        var image = new Image();
                        image.Height = 30;
                        image.Width = 30;
                        image.Margin = new Thickness(0, 0, 0, -6);
                        image.Source = new BitmapImage(new Uri(Emotions.GetUri(tokens[i]), UriKind.Relative));
                        var container = new InlineUIContainer();
                        container.Child = image;
                        (d as NewsBox).RichTitle.Inlines.Add(container);
                    }
                }
            }
        }

        private void NavigateToContentPage(object sender, GestureEventArgs e)
        {
            switch (NewsView.Model.FeedType)
            {
                case FeedType.PostBlog:
                case FeedType.ShareBlog:
                    this.Goto("BlogPage", NewsView.Model);
                    break;
                case FeedType.UpdateStatus:
                case FeedType.UploadPhoto:
                case FeedType.SharePhoto:
                case FeedType.ShareAlbum:
                case FeedType.ChangeProfilePhoto:
                case FeedType.BecomeFriends:
                case FeedType.ShareVideo:
                case FeedType.ShareLink:
                case FeedType.ShareMusic:
                    this.Goto("NewsPage", NewsView.Model);
                    break;
            }
        }

        private void Goto(string page, object arg)
        {
            if (ReactEvent)
            {
                this.NavigateTo(page, arg);
            }
        }
    }
}
